/*
 * 20. 骰子求和
 * 扔 n 个骰子，向上面的数字之和为 S。给定 Given n，请列出所有可能的 S 值及其相应的概率。
 * https://www.lintcode.com/problem/dices-sum/description
 * 
 * 样例
 * 给定 n = 1，返回 [ [1, 0.17], [2, 0.17], [3, 0.17], [4, 0.17], [5, 0.17], [6, 0.17]]。
 */
package dices_sum_1020;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class Dices_sum_1020 {

	/**
	 * @param n an integer
	 * @return a list of Map.Entry<sum, probability>
	 */
	
	static public List<Map.Entry<Integer, Double>> dicesSum(int n) {
		// Write your code here
		// Ps. new AbstractMap.SimpleEntry<Integer, Double>(sum, pro)
		// to create the pair

		int maxNum = n * 6;
		int minNum = n;
		int cnt = maxNum - minNum - 9;//运行次数
		int sum = 36 * cnt;	//总个数
		int[] cntnumber = {1,2,3,4,5,6,5,4,3,2,1};
		double[] result = new double[1000];
		if( n > 1) {
			for(int i = 0; i < cnt; ++i) {
				int tmp = minNum + i - 1;
				for(int j = 0; j < cntnumber.length; ++j) {
					result[ tmp ] += cntnumber[ j ]; 
					++tmp;
				}
			}
		} else {
			sum = 6;
			for(int i = 0; i < sum; ++i)
				result[ i ] = 1;
		}
		
//		System.out.println("maxNum:"+maxNum);
//		System.out.println("minNum:"+minNum);
//		System.out.println("cnt:"+cnt);
//		System.out.println("sum:"+sum);
//
//		for(int i = minNum -1; i < maxNum; ++i) {
//			System.out.print(i+1+":"+result[ i ] + " ");
//		}
//		System.out.println();
//		for(int i = minNum -1; i < maxNum; ++i) {
//			int tmp = (int) (result[ i ] / sum *100);
//			System.out.print(i+1+":"+(double)tmp/100+" ");
//		}

		List<Map.Entry<Integer, Double>> list = new ArrayList<>();
		for(int i = minNum-1; i < maxNum; ++i) {
			int tmp = (int) (result[ i ] / sum *100);
			Map<Integer,Double> map =new HashMap<>();
			map.put(i+1, (double)tmp/100);
			java.util.Iterator<Entry<Integer, Double>> it=map.entrySet().iterator();
			Map.Entry<Integer,Double> entry= (Map.Entry<Integer,Double>)it.next();
			list.add(entry);
		}
		return list;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		dicesSum(4);
	}

}
